package com.sanglh.tutorial.extra;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;

import com.sanglh.tutorial.R;
import com.sanglh.tutorial.common.util.SettingsUtils;
import com.sanglh.tutorial.common.view.SingleChoiceImageButtonGroup;
import com.sanglh.tutorial.common.view.SingleChoiceImageButtonGroup.OnSelectedChangeListener;

public class ExtraActivity extends Activity implements OnSelectedChangeListener {
	private static final int REQUEST_CODE_MIRROR = 1000;
	private Camera camera;
	private int cameraId = -1;
	private Object mSyncObject = new Object();
	private SingleChoiceImageButtonGroup groupCamera;
	private SingleChoiceImageButtonGroup groupPower;
	private BroadcastReceiver mWifiReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			int wifiState = intent
					.getIntExtra(WifiManager.EXTRA_WIFI_STATE, -1);
			findViewById(R.id.imgbtn_wifi).setSelected(
					wifiState == WifiManager.WIFI_STATE_ENABLED);
		}
	};

	private IntentFilter mWifiFilter;

	private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			int bluetoothState = intent.getIntExtra(
					BluetoothAdapter.EXTRA_STATE, -1);
			findViewById(R.id.imgbtn_bluetooth).setSelected(
					bluetoothState == BluetoothAdapter.STATE_CONNECTED);
		}
	};

	private IntentFilter mBluetoothFilter;

	@Override
	protected void onResume() {
		mWifiFilter = new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION);
		mBluetoothFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
		registerReceiver(mBluetoothReceiver, mBluetoothFilter);
		registerReceiver(mWifiReceiver, mWifiFilter);
		super.onResume();
	}

	@Override
	protected void onPause() {
		unregisterReceiver(mWifiReceiver);
		unregisterReceiver(mBluetoothReceiver);
		super.onPause();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_extra);
		findViewById(R.id.imgbtn_light).setEnabled(
				SettingsUtils.havingCameraHardware(this)
						&& SettingsUtils.isFlashSupported(this));
		findViewById(R.id.imgbtn_mirror).setEnabled(
				SettingsUtils.havingCameraHardware(this)
						&& SettingsUtils.havingFrontCameraHardware(this));
		findViewById(R.id.imgbtn_bluetooth).setEnabled(
				SettingsUtils.isBluetoothSupported());
		findViewById(R.id.imgbtn_bluetooth).setSelected(
				SettingsUtils.isBluetoothEnabled(this));
		findViewById(R.id.imgbtn_wifi).setSelected(
				SettingsUtils.isWifiEnabled(this));
		groupCamera = (SingleChoiceImageButtonGroup) findViewById(R.id.group_camera);
		groupCamera.setOnSelectedChangeListener(this);
		groupPower = (SingleChoiceImageButtonGroup) findViewById(R.id.group_power);
		groupPower.setOnSelectedChangeListener(this);
	}

	public void onLight(View view) {
		if (camera == null) {
			camera = SettingsUtils.getBackCamera();
		} else if (cameraId != CameraInfo.CAMERA_FACING_BACK) {
			SettingsUtils.releaseCamera(camera);
			camera = SettingsUtils.getBackCamera();
		}
		cameraId = CameraInfo.CAMERA_FACING_BACK;
		SettingsUtils.setFlashEnabled(ExtraActivity.this, camera,
				view.isSelected());
	}

	public void onMirror(View view) {
		if (camera != null) {
			SettingsUtils.releaseCamera(camera);
		}
		synchronized (mSyncObject) {
			MirrorActivity.showMirror(ExtraActivity.this, REQUEST_CODE_MIRROR);
		}

	}

	public void onWifi(View view) {
		boolean isSelected = !view.isSelected();
		view.setSelected(isSelected);
		SettingsUtils.setWifiEnabled(ExtraActivity.this, isSelected);

	}

	public void onBluetooth(View view) {
		boolean isSelected = !view.isSelected();
		view.setSelected(isSelected);
		SettingsUtils.setBluetoothEnabled(ExtraActivity.this, isSelected);

	}

	@Override
	protected void onStop() {
		SettingsUtils.releaseCamera(camera);
		super.onStop();
	}

	@Override
	public void onSelectedChanged(View view) {
		switch (view.getId()) {
		case R.id.imgbtn_light:
			onLight(view);
			break;
		case R.id.imgbtn_mirror:
			onMirror(view);
			break;
		case R.id.imgbtn_reboot:
			onReboot(view);
			break;
		default:
			break;
		}

	}


	private void onReboot(View view) {
		view.setSelected(true);
		SettingsUtils.powerReboot(ExtraActivity.this);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CODE_MIRROR:
			findViewById(R.id.imgbtn_mirror).setSelected(false);
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
	}
}
